<?php

require(DIRETORIO."app/Models/AppDesktopDAO.php");

class C_AppDesktop{

	protected static $lk = NULL;
	
	public static function init(){
		global $db_Mysql;

		self::$lk = $db_Mysql;
	}

	public static function getDB(){
		if(self::$lk == NUll)
			self::init();

		return self::$lk;
	}

	public static function getProjects($post){

		$objProjectDAO	= new ProjectDAO(self::getDB());
		$pagination		= (int)$post["page"];
		$objProjects	= $objProjectDAO->getProjects(0,$pagination);
		$numberRows		= $objProjectDAO->getNumberRows();

		$count			= 0;
		foreach($objProjects as $row){
			foreach($row->fieldsDB as $key => $nameField){
				$arrayReturn[$count][$key] = $row->$key;
			}

			$count++;
		}

		return json_encode(array($arrayReturn, $numberRows));
	}


	public static function getTasks($post){
		if(count($post) > 0){
			$objTaskDAO	= new AppDesktopDAO(self::getDB());
			$objTasks	= $objTaskDAO->getTasksUser($post["idUser"]);

			$count			= 0;
			foreach($objTasks as $key => $row){
				$arrayReturn[$count][$key] = $row;
				
				$count++;
			}
			
			return json_encode(array(true,$arrayReturn));
		}

		return json_encode(array(false, "Não foi possível listar as tarefas dos usuários!"));
	}

	public static function changeStateTask($post){
		$return = array(false,"Não foi possível trocar o estado da tarefa!");

		if(count($post) > 0){
			$objTaskDAO	= new TaskDAO(self::getDB());		
			$objTask	= new Task();
		
			$objTask->id			= $post['idTask'];
			$objTask->idTaskState	= $post['idTaskState'];

			if($objTaskDAO->save($objTask)){
				$textReturn = "O estado da tarefa foi trocado com sucesso!";

				$return =  array(true,$textReturn);
			}
		}

		return json_encode($return);
	}

	public static function login($post){
		if(count($post) > 0){
			$objUserDAO = new UserDAO(self::getDB());
			$idUser		= $objUserDAO->login($post['username'],$post['password']);

			if($idUser > 0){
				$tempObj = $objUserDAO->getUsers($idUser);
				$count			= 0;

				foreach($tempObj as $row){
					foreach($row->fieldsDB as $key => $nameField){
						$arrayReturn[$count][$key] = $row->$key;
					}

					$count++;
				}
				return json_encode(array(true, $arrayReturn));
			}else{
				return json_encode(array(false,NULL));
			}
		}

		return json_encode(array(false,NULL));
	}


}
?>